#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include"Queue.h"


typedef int BTDataType;

typedef struct BinaryTreeNode
{
	struct BinaryTreeNode* left;
	struct BinaryTreeNode* right;
	BTDataType data;
}BTNode;

BTNode* BuyNode(BTDataType x)
{
	BTNode* root = malloc(sizeof(BTNode));
	assert(root);
	root->data = x;
	root->left = NULL;
	root->right = NULL;
}

BTNode* CreatBinaryTree()
{
	BTNode* node1 = BuyNode(1);
	BTNode* node2 = BuyNode(2);
	BTNode* node3 = BuyNode(3);
	BTNode* node4 = BuyNode(4);
	BTNode* node5 = BuyNode(5);
	BTNode* node6 = BuyNode(6);

	node1->left = node2;
	node1->right = node4;
	node2->left = node3;
	node4->left = node5;
	node4->right = node6;

	return node1;
}
void DestryTree(BTNode* root)
{
	if (root == NULL)
		return;
	DestryTree(root->left);
	DestryTree(root->right);
	free(root);
}
//int BinaryTreeSize(BTNode* root)
//{
//	if (root == NULL)
//	{
//		return 0;
//	}
//	return 1 + BinaryTreeSize(root->left) + BinaryTreeSize(root->right);
//}
//
//int BinaryTreeLeafSize(BTNode* root)
//{
//	if (root == NULL)
//		return 0;
//	if (root->left == NULL && root->right == NULL)
//		return 1;
//	return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
//}

void LevelOrderTree(BTNode* root)
{
	Que q;
	QueueInit(&q);
	if (root)
		QueuePush(&q, root);
	while (!QueueEmpty(&q))
	{
		BTNode* front = QueueFront(&q);
		printf("%d ", front->data);
		QueuePop(&q);
		if (front->left)
		{
			QueuePush(&q, front->left);
		}
		if (front->right)
		{
			QueuePush(&q, front->right);
		}
	}
	QueueDestroy(&q);
}

int main()
{
	BTNode* root = CreatBinaryTree();
	//printf("%d ", BinaryTreeSize(root));
	//printf("%d ", BinaryTreeLeafSize(root));
	
	LevelOrderTree(root);
	return 0;
}